using SPI.AgentServer.Models;

namespace SPI.AgentServer.Services.Cache;

/// <summary>
/// 查询缓存服务接口
/// </summary>
public interface IQueryCacheService
{
    /// <summary>
    /// 获取缓存的查询结果
    /// </summary>
    Task<QueryResponse?> GetAsync(string query, string? databaseName = null);

    /// <summary>
    /// 设置查询结果缓存
    /// </summary>
    Task SetAsync(string query, QueryResponse response, string? databaseName = null, TimeSpan? expiration = null);

    /// <summary>
    /// 清除指定查询的缓存
    /// </summary>
    Task RemoveAsync(string query, string? databaseName = null);

    /// <summary>
    /// 清除所有缓存
    /// </summary>
    Task ClearAllAsync();

    /// <summary>
    /// 获取缓存统计信息
    /// </summary>
    Task<CacheStats> GetStatsAsync();
}

/// <summary>
/// 缓存统计信息
/// </summary>
public class CacheStats
{
    /// <summary>
    /// 缓存项数量
    /// </summary>
    public int Count { get; set; }

    /// <summary>
    /// 命中次数
    /// </summary>
    public long HitCount { get; set; }

    /// <summary>
    /// 未命中次数
    /// </summary>
    public long MissCount { get; set; }

    /// <summary>
    /// 命中率
    /// </summary>
    public double HitRate => HitCount + MissCount > 0 
        ? (double)HitCount / (HitCount + MissCount) * 100 
        : 0;
}





